<?php 
$dbname = getenv('MOPAAS_MYSQL17265_NAME');

$ip = getenv('MOPAAS_MYSQL17265_HOST');
$port = getenv('MOPAAS_MYSQL17265_PORT');
$host = $ip.":".$port;
$user = getenv('MOPAAS_MYSQL17265_USER'); 
$pass = getenv('MOPAAS_MYSQL17265_PASSWORD');

$DB_ADDRESS=$host;
$DB_USER=$user;
$DB_PASS=$pass;
$DB_NAME=$dbname;

$postUrl=$_SERVER["REQUEST_URI"]; 
$tag=$_POST['tag'];
$value=$_POST['value'];

if(strpos($postUrl,'storeavalue')){ 
  // Storing a Value

  // Get that tag
  $tag = trim($tag); 
  // Get the value 
  $value = trim($value);
  
  if(get_magic_quotes_gpc()){     //check if the worthless pile of crap magic quotes is enabled and if it is strip the slashes from the query
     $value=stripslashes($value);
  }
  $link = mysql_connect($DB_ADDRESS,$DB_USER,$DB_PASS);   //connect ot the MYSQL database
                mysql_select_db($DB_NAME,$link);                        //connect to the right DB
                if($link){
                        $result=mysql_query("insert into test(tag,value) values('".$tag."','".$value."')");  
                        if($result){
							echo json_encode(array("VALUE",$tag,array_merge(array(array("AFFECTED_ROWS ".mysql_affected_rows($link)))))); //if the query is anything but a SELECT it will return the array eve$
                                
                        } else {
							$result1=mysql_query("update test set value='".$value."' where tag='".$tag."'");
							if($result1){
								echo json_encode(array("VALUE",$tag,array_merge(array(array("AFFECTED_ROWS ".mysql_affected_rows($link)))))); //if the query is anything but a SELECT it will return the array eve$
                            }else{							
								echo json_encode(array("VALUE",$tag,array_merge(array(array(mysql_errno($link).": ".mysql_error($link)))))); //errors if the query is bad and spits the error back to the client
							}
                        }
						mysql_close($link);     //close the DB
                } else echo json_encode(array("VALUE",$tag,array_merge(array(array("ERROR Database Connection Failed"))))); //reports a DB connection failure
} else { 
  // Retrieving a Value

  // Get that tag
  $tag = trim($tag); 
  $link = mysql_connect($DB_ADDRESS,$DB_USER,$DB_PASS);   //connect ot the MYSQL database
                mysql_select_db($DB_NAME,$link);                        //connect to the right DB
                if($link){
                        $result=mysql_query("select value from  test where tag='".$tag."'");  
                        if($result){
                          
                          if(mysql_num_rows($result) == 1){
                           		$row = mysql_fetch_assoc($result);							                          
                          		echo json_encode(array("VALUE",$tag,array($row["value"]))); //sends out a JSON result with merged output data
                          }else
                            echo json_encode(array("VALUE",$tag,array(""))); //sends out a JSON result with merged output data
                                
                        } else {
														
								echo json_encode(array("VALUE",$tag,array_merge(array(mysql_errno($link).": ".mysql_error($link))))); //errors if the query is bad and spits the error back to the client
							
                        }
						mysql_close($link);     //close the DB
                } else echo json_encode(array("VALUE",$tag,array_merge(array("ERROR Database Connection Failed")))); //reports a DB connection failure

} 

?> 